package com.ftdigital.primemovers_operators.fragment;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.MyApplication;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.model.AddOilInfo;
import com.ftdigital.primemovers_operators.util.qrcode.MipcaActivityCapture;
import com.ftdigital.primemovers_operators.util.view.RoundImageView;
import com.squareup.picasso.Picasso;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.ViewsById;

import java.util.List;

/**
 * 首页
 */
@EFragment(R.layout.fragment_home)
public class HomeFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewsById({R.id.username, R.id.calls, R.id.userno, R.id.company, R.id.station_name})
    List<TextView> detailList;

    @ViewById(R.id.headimg)
    RoundImageView headimg;
    private AsyncTask<String, Void, String> mCacheTask;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.GONE);
        topTitle.setText(getResources().getString(R.string.title_userpage));
//        mCacheTask = new CacheTask().execute("You can't bully me");


//        Log.i(GlobalData.TAG, MyApplication.userInfo.getData().getUrl());
//        MyApplication.userInfo.getData().getUrl();

        detailList.get(0).setText(MyApplication.userInfo.getData().getUser_name());
        detailList.get(1).setText(MyApplication.userInfo.getData().getMobile());
        detailList.get(2).setText(MyApplication.userInfo.getData().getEmp_no());
        detailList.get(3).setText(MyApplication.userInfo.getData().getSupplier_name());
        detailList.get(4).setText(MyApplication.userInfo.getData().getGasstation_name());
        Picasso.with(getActivity()).load(MyApplication.userInfo.getData().getUrl()).resize(50, 50).error(R.drawable.user_headimg).into(headimg);
    }


    @Override
    protected String logInfo(String string) {
        return string;
    }
    private final static int SCANNIN_GREQUEST_CODE = 1;
    @Click({R.id.no_cellphone_btn, R.id.qr_code_btn})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.no_cellphone_btn:
                MainActivity.getInstance().switchFragment(this, new NoMobileValidationFragment_());
                break;
            case R.id.qr_code_btn:
                Intent intent = new Intent();
                intent.setClass(MainActivity.getInstance(), MipcaActivityCapture.class);
                MainActivity.getInstance().startActivity(intent);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
                break;
        }
    }

    AddOilInfo addOilInfo;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SCANNIN_GREQUEST_CODE:
                if(resultCode == getActivity().RESULT_OK){
                    Bundle bundle = data.getExtras();
                    addOilInfo = (AddOilInfo) bundle.getSerializable("addOilInfo");
                    //显示扫描到的内容
                    MainActivity.getInstance().getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.content_none_bottom, InfoEnsureFragment_.builder()
                                    .addOilInfo(addOilInfo)
                                    .build()).addToBackStack(null).commit();
//                    Toast.makeText(this, "main:"+bundle.getString("result"),Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
